/**
 * 
 */
package com.witframework.web.extremetable;

import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Row;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.RowBuilder;
import org.extremecomponents.util.HtmlBuilder;

/**
 * @author wuxinyang
 * Jun 24, 2009 12:42:12 PM
 * 
 */
public class WsRowBuilder extends RowBuilder{
	private HtmlBuilder html;
    private TableModel model;
    private Row row;

    public WsRowBuilder(TableModel model) {
        this(new HtmlBuilder(), model);
    }

    public WsRowBuilder(HtmlBuilder html, TableModel model) {
    	super(html,model);
        this.html = html;
        this.model = model;
        this.row = model.getRowHandler().getRow();
    }

    public HtmlBuilder getHtmlBuilder() {
        return html;
    }

    protected TableModel getTableModel() {
        return model;
    }

    protected Row getRow() {
        return row;
    }

    @Override
	public void rowStart() {
        html.tr(1);      
        styleClass();
        style();
        onclick();
        onmouseover();
        onmouseout();      
       
    }
   
    public void rowClose() {
    	html.close();     
    }
    
    public void rowEnd() {
        html.trEnd(1);
    }

    public void style() {
    	if(row!=null){
    		String style = row.getStyle();
    		html.style(style);
    	}else{
    		html.style(BuilderConstants.ROW_EVEN_CSS);
    	}
    }

    public void styleClass() {
        String styleClass = getStyleClass();
        html.styleClass(styleClass);
    }

    public void onclick() {
    	if(row!=null){
    		String onclick = row.getOnclick();
    		html.onclick(onclick);
    	}
    }

    public void onmouseover() {
    	if(row!=null){
	        boolean highlightRow = row.isHighlightRow();
	        if (highlightRow) {
	            String highlightClass = row.getHighlightClass();
	            if (StringUtils.isNotBlank(row.getOnmouseover())) {
	                html.onmouseover("this.className='" + highlightClass + "';" + row.getOnmouseover());
	            } else {
	                html.onmouseover("this.className='" + highlightClass + "'");
	            }
	        } else {
	            html.onmouseover(row.getOnmouseover());
	        }
    	}
    }

    public void onmouseout() {
    	if(row!=null){
	        boolean highlightRow = row.isHighlightRow();
	        if (highlightRow) {
	            String styleClass = getStyleClass();
	            if (StringUtils.isNotBlank(row.getOnmouseout())) {
	                html.onmouseout("this.className='" + styleClass + "';" + row.getOnmouseout());
	            } else {
	                html.onmouseout("this.className='" + styleClass + "'");
	            }
	        } else {
	            html.onmouseout(row.getOnmouseout());
	        }
    	}
    }

    protected String getStyleClass() {
    	
    	if(row!=null){
    		String styleClass = row.getStyleClass();
	        if (StringUtils.isNotBlank(styleClass)) {
	            return styleClass;
	        }
	
	        if (model.getRowHandler().isRowEven()) {
	            return BuilderConstants.ROW_EVEN_CSS;
	        }
    	}
        return BuilderConstants.ROW_ODD_CSS;
    }
    
    public String toString() {
        return html.toString();
    }
}
